package com.mrjy.weixin.bo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.mrjy.common.entity.WeixinMenu;
import com.mrjy.common.entity.WeixinMenuGroup;
import com.mrjy.weixin.dao.IWeixinMenuDao;
import com.mrjy.weixin.util.CopyList;

@Service("weixinMenuBo")
public class WeixinMenuBo implements IWeixinMenuBo {
	
	@Resource
	private IWeixinMenuDao weixinMenuDao;

	@Override
	public Map<String, Object> weixinMenuGroupList(WeixinMenuGroup weixinMenuGroup) {	
		Map<String,Object> map = new HashMap<String,Object>();
		List<WeixinMenuGroup> weixinMenuGroupList = weixinMenuDao.queryPageWeiXinMenuGroup(weixinMenuGroup);
//		if(weixinMenuGroupList != null){
////			for(int i = 0;i<weixinMenuGroupList.size();i++){
////				Date createtime = weixinMenuGroupList.get(i).getCreatetime();
//////				if(createtime!=null ){
//////					String createtimeStr = DateUtil.dateToString(createtime, 2);
//////					weixinMenuGroupList.get(i).setCreatetimeStr(createtimeStr);				
//////				}else{
//////					weixinMenuGroupList.get(i).setCreatetimeStr(null);		
//////				}				
////			}
//
//		}
		map.put("total", weixinMenuDao.countWeiXinMenuGroup(weixinMenuGroup));
		map.put("rows", weixinMenuGroupList);
		return map;
	}

	@Override
	public WeixinMenuGroup queryById(int id) {
		return weixinMenuDao.queryById(id);
	}

	@Override
	public List<Map<String,Object>> queryMenuTree(int id) {
		List<WeixinMenu> list = weixinMenuDao.queryMenuTree(id);
		List<Map<String,Object>> re = this.makeTreeData(list);
		return re;
	}
	
	@Override
	public List<Map<String, Object>> makeTreeData(List<WeixinMenu> list) {
		if(list==null||list.size()==0){
			return null;
		}
		List<Map<String,Object>> re = new ArrayList<Map<String,Object>>();
		for(Iterator<WeixinMenu>  it=list.iterator();it.hasNext();){
			WeixinMenu m = it.next();
			if(m.getParentId()==0){
				Map<String,Object> parentmap = new HashMap<String, Object>();
				parentmap.put("text", m.getName());
				parentmap.put("id", m.getId());
				parentmap.put("attributes", m);
				re.add(parentmap);
				it.remove();
			}
        }
		for(int i = 0 ; i < re.size(); i ++ ){
			Map<String,Object> im = re.get(i);
			List<Map<String,Object>> children = new ArrayList<Map<String,Object>>();
			for(int j = 0 ; j < list.size(); j ++ ){
				WeixinMenu jm = list.get(j);
				Map<String,Object> childrenmap = new HashMap<String, Object>();
				childrenmap.put("text", jm.getName());
				childrenmap.put("id", jm.getId());
				childrenmap.put("attributes", jm);
				if( im.get("id").equals(jm.getParentId())  ){
					children.add(childrenmap);
				}
			}
			im.put("children", children);
		}
		return re;
	}

	@Override
	public Integer deleteMenuGroup(int[] ids) {
		weixinMenuDao.deleteMenuByGid(ids);
		return weixinMenuDao.deleteGroupById(ids);
	}

	@Override
	public int addMenuGroup(WeixinMenuGroup weixinMenuGroup) {
		return weixinMenuDao.addMenuGroup(weixinMenuGroup);
	}

	@Override
	public int addMenuByA(List<WeixinMenu> list, WeixinMenuGroup weixinMenuGroup) {
		int o = 0;
		List<Map<String, Object>> datalist =  this.makeTreeData(list);
		for( int i = 0 ; i < datalist.size() ; i ++ ){
			WeixinMenu wxm = (WeixinMenu) datalist.get(i).get("attributes");
			wxm.setSort(i);
			wxm.setGid(weixinMenuGroup.getId());
			o += weixinMenuDao.addMenu(wxm);
			List<Map<String,Object>> childrenlist = (List<Map<String, Object>>) datalist.get(i).get("children");
			for( int j = 0 ; j < childrenlist.size() ; j ++ ){
				WeixinMenu wxmc = (WeixinMenu) childrenlist.get(j).get("attributes");
				wxmc.setGid(weixinMenuGroup.getId());
				wxmc.setParentId(wxm.getId());
				wxmc.setSort(j);
				o += weixinMenuDao.addMenu(wxmc);
			}
			
		}
		return o;
	}

	@Override
	public int addMenuByU(WeixinMenu weixinMenu) {
		weixinMenuDao.addMenuByU(weixinMenu);
		return weixinMenu.getId();
	}

	@Override
	public int updateMenuByU(WeixinMenu weixinMenu) {
		return weixinMenuDao.updateMenuByU(weixinMenu);
	}

	@Override
	public int updateGroupByUId(WeixinMenuGroup weixinMenuGroup) {
		return weixinMenuDao.updateGroupByUId(weixinMenuGroup);
	}

	@Override
	public int deleteMenu(int id) {
		return weixinMenuDao.deleteMenu(id);
	}

	@Override
	public List<WeixinMenu> deleteMenuByS(List<WeixinMenu> list, String strfalg) {
		int iii = 0 ;
		for (Iterator<WeixinMenu> iterator = list.iterator(); iterator.hasNext();) {
			WeixinMenu wxm = iterator.next();
			if( strfalg.equals( wxm.getStrfalg() ) ){
				if( 0 == wxm.getParentId()){
					iii = wxm.getId();
				}
				iterator.remove();
			}
			
		}
		if(iii!=0){
			this.deleteChildrenMenuBys(list,iii);
		}
		return list;
	}

	@Override
	public void deleteChildrenMenuBys(List<WeixinMenu> list, int iii) {
		for (Iterator<WeixinMenu> iterator = list.iterator(); iterator.hasNext();) {
			WeixinMenu wxm = iterator.next();
			if( iii == wxm.getParentId() ){
				iterator.remove();
			}
			
		}
	}
	
}
